package com.bw.shopping.mvvm.viewmodel;


import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.blankj.utilcode.util.LogUtils;

import com.bw.mvvm.entity.BaseEntity;
import com.bw.mvvm.entity.PayEntity;
import com.bw.mvvm.fleld.UIField;
import com.bw.mvvm.mvvm.viewmodel.BaseViewModel;
import com.bw.mvvm.utils.SpUtils;
import com.bw.shopping.R;
import com.bw.shopping.adapter.CartAdapter;
import com.bw.shopping.mvvm.model.ShoppingModel;
import com.bw.mvvm.entity.CartEntity;
import com.bw.shopping.mvvm.model.entity.SubmitEntity;
import com.bw.shopping.mvvm.view.OrderFormActivity;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.RequestBody;

public class ShoppingViewModel extends BaseViewModel<ShoppingModel>  {
    public int count = 0;
    public boolean isClick = false;

    public String userId = SpUtils.getUserId();
//    public ObservableField<Boolean> isclick=new ObservableField<>(false);
    public ObservableField<String> money = new ObservableField<>("合计:￥0.00");
    public CartAdapter cartadapter = new CartAdapter();
    public List<CartEntity.DataBean> datas = new ArrayList<>();
    public ObservableField<String> compile = new ObservableField<>("编辑");
    public ObservableField<String> settle = new ObservableField<>("结算");

    @Override
    protected void result(BaseEntity entity) {
        LogUtils.e(entity.getMessage());
        if (entity instanceof CartEntity){
            CartEntity entity1 = (CartEntity) entity;
            datas = entity1.getData();
            cartadapter.setNewInstance(entity1.getData());
        }
        cartadapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull @NotNull BaseQuickAdapter adapter, @NonNull @NotNull View view, int position) {
                if (view.getId() == R.id.cart_rb){
                    cartadapter.getData().get(position).setClick(!cartadapter.getData().get(position).isClick());
                    count += Integer.valueOf(datas.get(position).getGoods_price());
                }
                mySetMoney();
                cartadapter.notifyDataSetChanged();
            }
        });
        if (entity.getMessage().equals("购物车提交成功")){

            if (entity instanceof SubmitEntity){
                SubmitEntity submitEntity = (SubmitEntity) entity;
                EventBus.getDefault().postSticky(submitEntity);
            }
            List<String> cartIdList = new ArrayList<>();
            for (int i = 0; i < datas.size(); i++) {
                if (datas.get(i).isClick()){
                    cartIdList.add(""+datas.get(i).getId());
                }
            }
            HashMap<String,Object> delMap = new HashMap<>();
            delMap.put("cartIdList",cartIdList);
            RequestBody body1 = m.createBody(delMap);
            m.delCart(body1).subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(this);

            HashMap<String,Object> map = new HashMap<>();
            map.put(UIField.PAGECS, OrderFormActivity.class);
            startPage(map);
        }
        if (entity.getMessage().equals("删除购物车成功")){
            HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("id",453);
            m.requestDeta(hashMap).subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(this);
        }
        if (entity.getMessage().equals("添加购物车成功")){
            HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("id",453);
            m.requestDeta(hashMap).subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(this);
        }
        if (entity.getMessage().equals("订单支付成功")){
            HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("id",453);
            m.requestDeta(hashMap).subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(this);
        }
    }

    @Subscribe(sticky = true)
    public void getPay(PayEntity entity){
        LogUtils.e(entity.message);
        if (entity.getMessage().equals("支付成功")){
            HashMap<String,Object> map = new HashMap<>();
            map.put("orderId",entity.orderId);
            RequestBody body = m.createBody(map);
            m.requestPay(body).subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(this);
        }
    }

    @Subscribe(sticky = true)
    public void getEventBus(int index){
        LogUtils.e(index);
        if (index == 100){
            cartadapter.notifyDataSetChanged();
        }
    }

    public void mySetMoney(){
        Double myMoney = 0d;
        for (int i = 0; i < datas.size(); i++) {
            if (datas.get(i).isClick()){
                String goods_price = datas.get(i).getGoods_price();
                Double aDouble = Double.valueOf(goods_price);
                myMoney += aDouble;
            }
        }
        money.set("合计:￥"+myMoney);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        EventBus.getDefault().register(this);
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("id",453);
        m.requestDeta(hashMap).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }

    public void compileClick(){
        settle.set("删除");
    }

    public void onAll(){
        count = 0;
        if (isClick){
            isClick = false;
            for (int i = 0; i < datas.size(); i++) {
                datas.get(i).setClick(false);
            }
        }else {
            isClick = true;
            for (int i = 0; i < datas.size(); i++) {
                datas.get(i).setClick(true);
                count += Integer.valueOf(datas.get(i).getGoods_price());
            }
        }
        money.set("合计:￥"+count);
        cartadapter.notifyDataSetChanged();
    }

    public void setTitle(){
        List<HashMap<String,Object>> mapList = new ArrayList<>();

        for (int i = 0; i < datas.size(); i++) {
            HashMap<String,Object> goodsMap = new HashMap<>();
            if (datas.get(i).isClick()){
                goodsMap.put("goods_count",1);
                goodsMap.put("goods_desc",datas.get(i).getGoods_desc());
                goodsMap.put("goods_icon",datas.get(i).getGoods_icon());
                goodsMap.put("goods_id",datas.get(i).getGoods_id());
                goodsMap.put("goods_price",datas.get(i).getGoods_price());
                goodsMap.put("goods_sku",datas.get(i).getGoods_sku());
                goodsMap.put("id",datas.get(i).getId());
                goodsMap.put("order_id",0);
                mapList.add(goodsMap);
            }
        }

        HashMap<String,Object> listMap = new HashMap<>();
        listMap.put("goodsList",mapList);
        listMap.put("totalPrice",count);
        listMap.put("userId",userId);
        RequestBody body = m.createBody(listMap);
        m.submitCart(body).subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(this);

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


}
